var skuId = sessionStorage.getItem("sku_id");
var spuImgCount = 0;
$.ajax({
    url: root.goodsInter.goods_temporaryProductDetail,
    cache: false, //禁用缓存
    type: "POST",
    dataType: "json",
    data: {
        skuId: skuId
    },
    success: function (data) {
        if (data.errorCode == 0) {
            sessionStorage.setItem("procureskuInfo",JSON.stringify(data));
            sessionStorage.setItem("isSave", 1);// 修改是1 添加是0
            console.log(data);
            var obj = data.data;
            $("#skuCode").html(obj.sku_code);
            $("#skuName").html(obj.sku_name);
            $("#specifications").html(obj.specifications);
            $("#category").html(data.category);
            $("#code").html(obj.product_code);
            $("#oldSkuName").html(obj.old_sku_name);
            $("#oldSpecifications").html(obj.old_specifications);
            if (data.oldCategory != null) {
                $("#oldCategory").html(data.oldCategory);
            } else {
                $("#oldCategory").html('');
            }
            $("#brand").html(obj.brand_name);

            $("#unit").html(obj.unit);
            $("#customerName").html(obj.customer_name);

            if(obj.detail!=null){
                ue.ready(function() {
                    ue.setContent(obj.detail);
                });
            }else {
                ue.ready(function() {
                    ue.setContent("");
                });
            }

            $("#skucode").val(obj.sku_code)

            if(obj.mini_order!=null){
                $("#miniOrder").html(obj.mini_order)
            }else {
                $("#miniOrder").html("1")
            }


            $("#costPrice").html(obj.cost_price + ' 元/' + obj.unit);
            $("#vipPrice").html(obj.price + ' 元/' + obj.unit);

            $("#arrivalCycle").html(obj.arrival_cycle + '天');
        }
        //图片处理

        sessionStorage.setItem("spuInfo",JSON.stringify(data));
        sessionStorage.setItem("isSave", 1);// 修改是1 添加是0
        var procureImgCache = {};
        var spuResult = JSON.parse(sessionStorage.getItem("spuInfo"));
        /*-----------------------------------图片处理开始----------------------------------------*/
        if(sessionStorage.getItem("procureImgCache")){
            var spuInfoImg = JSON.parse(sessionStorage.getItem("procureImgCache"));
            var spuInfoImgArr = Object.keys(spuInfoImg);
            for (var i = 0; i < spuInfoImgArr.length; i++) {
                var img = "img" + (i + 1);
                $(".imgTable .imgboxUl").append('<div class="f-l ml-5"><span id="spuImg' + (i + 1) + '" class="spu_add_img"><img onclick="picShow(\'查看图片\',\'picShow.html\',\'' + spuInfoImg[img].imgUrl + '\')" src="' + spuInfoImg[img].imgUrl + '" width="150" height="150"></span><span id="spuImgId' + (i + 1) + '" style="display: none">' + spuInfoImg[img].imgId + '</span><span id="spuImgUrl' + (i + 1) + '" style="display: none">' + spuInfoImg[img].imgUrl + '</span><div id="btnBox' + (i + 1) + '" class="pt-10 updataBtn"><a onclick="editPic(' + (i + 1) + ', false)" class="pd-20">修改</a><a onclick="deletePic(' + (i + 1) + ')" class="pd-20">删除</a></div><div class="pd-20">排序&nbsp;&nbsp;&nbsp;<input type="text" class="input-text radius" onchange="checkRepeat(this, ' + (i + 1) + ')" value="' + spuInfoImg[img].imgSort + '" id="spuImgSort' + (i + 1) + '" name="imgSort" style="width:40px"></div></div>');
            }

        }else {
            var spuInfoImg = spuResult.data.tempPurchaseSkuImg;
            console.log(spuInfoImg)
            spuImgCount = spuInfoImg.length;
            for(var i = 0; i < spuInfoImg.length; i++){
                $(".imgTable .imgboxUl").append('<div class="f-l ml-5"><span id="spuImg' + (i + 1) + '" class="spu_add_img"><img onclick="picShow(\'查看图片\',\'picShow.html\',\'' + spuInfoImg[i].imgUrl + '\')" src="' + spuInfoImg[i].imgUrl + '" width="150" height="150"></span><span id="spuImgId' + (i + 1) + '" style="display: none">' + spuInfoImg[i].imgId + '</span><span id="spuImgUrl' + (i + 1) + '" style="display: none">' + spuInfoImg[i].imgUrl + '</span><div id="btnBox' + (i + 1) + '" class="pt-10 updataBtn"><a onclick="editPic(' + (i + 1) + ', false)" class="pd-20">修改</a><a onclick="deletePic(' + (i + 1) + ')" class="pd-20">删除</a></div><div class="pd-20">排序&nbsp;&nbsp;&nbsp;<input type="text" class="input-text radius" onchange="checkRepeat(this, ' + (i + 1) + ')" value="' + spuInfoImg[i].imgSort + '" id="spuImgSort' + (i + 1) + '" name="imgSort" style="width:40px"></div></div>');
                procureImgCache["img" + (i + 1)] = {
                    imgId: spuInfoImg[i].imgId,
                    imgUrl: spuInfoImg[i].imgUrl,
                    imgSort: spuInfoImg[i].imgSort
                };

            }
            sessionStorage.setItem("procureImgCache", JSON.stringify(procureImgCache));
            sessionStorage.setItem("imgLength", JSON.stringify(spuInfoImg.length));
        }
        /*-----------------------------------图片处理结束----------------------------------------*/
    }
});


$("#customerName").mouseover(function () {
    $("#mouseBoxCus").show();
    $("#mouseBoxCus").html($("#customerName").html());
}).mouseout(function () {
    $("#mouseBoxCus").hide();
});

function makeSure() {
    var spuImgs = [];
    var imgLength= document.getElementsByClassName("imgboxUl")[0].children.length;
    for (var i = 1; i <= imgLength; i++) {
        if($("#spuImg" + i).children().length != 0){
            spuImgs.push( {
                "imgId": $("#spuImgId" + i).html(),
                "imgSort": $("#spuImgSort" + i).val(),
                "imgUrl": $("#spuImgUrl" + i).html().split("?")[0],
                "skuId":skuId
            })
        }
    }
    $.ajax({
        contentType: 'application/json',
        url: root.goodsInter.goods_updateTemporaryProduct,
        cache: false, //禁用缓存
        type: "POST",
        data: JSON.stringify({
            skuId: skuId,
            skuName: $("#skuName").html(),
            specifications: $("#specifications").html(),
            "miniOrder":$("#miniOrder").html(),
            "tempPurchaseSkuImg":spuImgs,
            "productDetail":UE.getEditor('editor').getContent(),
            "skuCode":$("#skucode").val(),
            "creation_by":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID
        }),
        success: function (data) {
            if (data.errorCode == 0) {
                parent.getTemporaryGoodsList(parent.comPageNum);
                layer.msg("修改成功",{icon: 6,time:1000},function () {
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                });
            }
        }
    });
}

//图片信息
function editPic(imgSort, isadd) {
    sessionStorage.setItem("isadd",isadd);
    sessionStorage.setItem("imgSort",imgSort);
    layer_show("修改图片","procurePic.html","1100","600","100","200");
}
function addPic(imgSort, isadd) {
    sessionStorage.setItem("isadd",isadd);
    sessionStorage.setItem("imgSort",imgSort);
    layer_show("添加图片","procurePic.html","1100","600","100","200");
}
function deletePic(imgSort) {
    var popId = layer.confirm('确认要删除吗？',function(index){
        $(".addSpuImgbox").css({
            display:"block"
        })
        var procureImgCache = JSON.parse(sessionStorage.getItem("procureImgCache"));
        var spuInfoImgArr = Object.keys(procureImgCache);
        if(sessionStorage.getItem("procureImgCache")){
            for (var i = 0; i < spuInfoImgArr.length; i++) {
                if(imgSort == (i + 1)){
                    for (var j = imgSort; j < spuInfoImgArr.length; j++) {
                        procureImgCache["img" + (j + 1)].imgSort = j;
                        procureImgCache["img" + j] = procureImgCache["img" + (j + 1)];
                    }
                    delete procureImgCache["img" + spuInfoImgArr.length];
                    break;
                }
            }
            sessionStorage.setItem("procureImgCache",JSON.stringify(procureImgCache));
        }
        //var index = parent.layer.getFrameIndex(popId);
        layer.close(popId);
        addSpuPic();
    });
}

$("#addSpuImg").click(function () {
    var imgLength= document.getElementsByClassName("imgboxUl")[0].children.length;
    sessionStorage.setItem("imgLength", JSON.stringify(imgLength));
    if(sessionStorage.getItem("procureImgCache")){
        var getprocureImgCache = sessionStorage.getItem("procureImgCache");
        var spuInfoImg = JSON.parse(getprocureImgCache);
        var spuInfoImgArr = Object.keys(spuInfoImg);
        addPic((spuInfoImgArr.length+1), true);
    }
});
//渲染图片渲染函数
function addSpuPic() {
    $(".imgTable .imgboxUl").html("");
    if(sessionStorage.getItem("procureImgCache")){
        var spuInfoImg = JSON.parse(sessionStorage.getItem("procureImgCache"));
        var spuInfoImgArr = Object.keys(spuInfoImg);
        for (var i = 0; i < spuInfoImgArr.length; i++) {
            var img = "img" + (i + 1);
            $(".imgTable .imgboxUl").append('<div class="f-l ml-5"><span id="spuImg' + (i + 1) + '" class="spu_add_img"><img onclick="picShow(\'查看图片\',\'picShow.html\',\'' + spuInfoImg[img].imgUrl + '\')" src="' + spuInfoImg[img].imgUrl + '" width="150" height="150"></span><span id="spuImgId' + (i + 1) + '" style="display: none">' + spuInfoImg[img].imgId + '</span><span id="spuImgUrl' + (i + 1) + '" style="display: none">' + spuInfoImg[img].imgUrl + '</span><div id="btnBox' + (i + 1) + '" class="pt-10 updataBtn"><a onclick="editPic(' + (i + 1) + ', false)" class="pd-20">修改</a><a onclick="deletePic(' + (i + 1) + ')" class="pd-20">删除</a></div><div class="pd-20">排序&nbsp;&nbsp;&nbsp;<input type="text" class="input-text radius" onchange="checkRepeat(this, ' + (i + 1) + ')" value="' + spuInfoImg[img].imgSort + '" id="spuImgSort' + (i + 1) + '" name="imgSort" style="width:40px"></div></div>');
        }
    }
}

